package ru.rchervot.sanasar.client.view;

import ru.rchervot.sanasar.client.event.EventPageChange;
import ru.rchervot.sanasar.client.event.HandlerPageChange;
import ru.rchervot.sanasar.client.model.ResultsPage;
import ru.rchervot.sanasar.client.model.SearchResultsItem;
import ru.rchervot.sanasar.client.rpc.GetPage;
import ru.rchervot.sanasar.client.rpc.GotPage;
import ru.rchervot.sanasar.client.rpc.RPCServiceAsync;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;

public abstract class AbstractResultsFooterPresenter implements ResultsFooterPresenter {
	private final HandlerManager eventBus;
	private final RPCServiceAsync service;
	private final Display display;
	private ResultsPage<SearchResultsItem> currentPage;
	
	public AbstractResultsFooterPresenter(Display display, HandlerManager eventBus,
			RPCServiceAsync service) {
		this.eventBus = eventBus;
		this.display = display;
		this.service = service;
		
		listenForPageChange();
		handlePageFirstClick();
		handlePagePrevClick();
		handlePageNextClick();
		handlePageLastClick();
		
  }
	
	@Override
	public void bindDisplay() {
		display.setPresenter(this);
	}
	
  @Override
	public Display getDisplay() {
		return display;
	}
  
  private void listenForPageChange() {
		eventBus.addHandler(EventPageChange.getType(),
				new HandlerPageChange<SearchResultsItem>() {
			@Override
			public void onPageChange(
					EventPageChange<SearchResultsItem> event) {
				currentPage = event.getPage();
				
				if (event.isStartPage()) {
					disableButtons();
				} else {
					if (event.getPage().isEmpty()) {
						disableButtons();
					} else {
						setButtonsVisibility(event.getPage());					
					}					
				}
				
			}
		});
	}
  
  private void disableButtons() {
  	display.setEnabledButtonFirst(false);
		display.setEnabledButtonPrev(false);
		display.setEnabledButtonNext(false);
		display.setEnabledButtonLast(false);
  }
  
  private void setButtonsVisibility(ResultsPage<SearchResultsItem> page) {
		if (page.hasPrevious()) {
			display.setEnabledButtonFirst(true);
			display.setEnabledButtonPrev(true);
		} else {
			display.setEnabledButtonFirst(false);
			display.setEnabledButtonPrev(false);
		}

		if (page.hasNext()) {
			display.setEnabledButtonNext(true);
			display.setEnabledButtonLast(true);
		} else {
			display.setEnabledButtonNext(false);
			display.setEnabledButtonLast(false);
		}
	}

  
  private void handlePageFirstClick() {
  	display.getPageFirstButton().addClickHandler(new ClickHandler() {
  		@Override
  		public void onClick(ClickEvent event) {
  			service.execute(
  					new GetPage(currentPage.getQueryID(),
  							currentPage.getPageFirstOffset()),
  							new GotPage<SearchResultsItem>() {
  						@Override
  						public void got(
  								ResultsPage<SearchResultsItem> resultsPage) {
  						}
  					});
  		}
  	});
  }
  
  private void handlePagePrevClick() {
  	display.getPagePrevButton().addClickHandler(new ClickHandler() {
  		@Override
  		public void onClick(ClickEvent event) {
  			service.execute(
  					new GetPage(currentPage.getQueryID(),
  							currentPage.getPagePrevOffset()),
  							new GotPage<SearchResultsItem>() {
  						@Override
  						public void got(
  								ResultsPage<SearchResultsItem> resultsPage) {
  						}
  					});
  		}
  	});
  }
  
  private void handlePageNextClick() {
  	display.getPageNextButton().addClickHandler(new ClickHandler() {
  		@Override
  		public void onClick(ClickEvent event) {
  			service.execute(
  					new GetPage(currentPage.getQueryID(),
  							currentPage.getPageNextOffset()),
  							new GotPage<SearchResultsItem>() {
  						@Override
  						public void got(
  								ResultsPage<SearchResultsItem> resultsPage) {
  						}
  					});
  		}
  	});
  }
  
  private void handlePageLastClick() {
  	display.getPageLastButton().addClickHandler(new ClickHandler() {
  		@Override
  		public void onClick(ClickEvent event) {
  			service.execute(
  					new GetPage(currentPage.getQueryID(),
  							currentPage.getPageLastOffset()),
  							new GotPage<SearchResultsItem>() {
  						@Override
  						public void got(
  								ResultsPage<SearchResultsItem> resultsPage) {
  						}
  					});
  		}
  	});
  }
}
